package com.turnengine.client.local.turn.bean;

import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.local.turn.enums.TurnUpdating;
import com.turnengine.client.local.turn.enums.TurnsEnabled;
import java.io.IOException;

/**
 * The Turn Summary Serializer.
 */
public class TurnSummarySerializer implements ITurnSummarySerializer {

	@Override
	public ITurnSummary readObject(IDataReader reader) throws IOException {

		// Read fields
		int number = reader.readInt();
		TurnsEnabled enabled = reader.readEnum(TurnsEnabled.class, true);
		TurnUpdating updating = reader.readEnum(TurnUpdating.class, true);
		long timeToNextTurn = reader.readLong();
		long timeOfNextTurn = reader.readLong();
		long currentTime = reader.readLong();

		// Create object
		ITurnSummary object = new TurnSummary();
		object.setNumber(number);
		object.setEnabled(enabled);
		object.setUpdating(updating);
		object.setTimeToNextTurn(timeToNextTurn);
		object.setTimeOfNextTurn(timeOfNextTurn);
		object.setCurrentTime(currentTime);
		return object;
	}

	public void writeObject(IDataWriter writer, ITurnSummary object) throws IOException {

		// Get fields
		int number = object.getNumber();
		TurnsEnabled enabled = object.getEnabled();
		TurnUpdating updating = object.getUpdating();
		long timeToNextTurn = object.getTimeToNextTurn();
		long timeOfNextTurn = object.getTimeOfNextTurn();
		long currentTime = object.getCurrentTime();

		// Write fields
		writer.writeInt(number);
		writer.writeEnum(enabled, true);
		writer.writeEnum(updating, true);
		writer.writeLong(timeToNextTurn);
		writer.writeLong(timeOfNextTurn);
		writer.writeLong(currentTime);
	}
}
